﻿using System;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Newtonsoft.Json;
using System.Text;
using System.IO;

namespace nyxwin.Data.Cache
{
    public class CacheProvider
    {
        #region Fields
        static CacheProvider _instance = null;
        IsolatedStorageFile _file = null;
        #endregion

        #region Instance
        public static CacheProvider Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new CacheProvider();
                }
                return _instance;
            }
        }
        #endregion

        #region Construction
        static CacheProvider()
        {
        } 

        CacheProvider()
        {
            _file = IsolatedStorageFile.GetUserStoreForApplication();
        }
        #endregion

        #region Flush
        public void Flush(object graph, CacheType cacheType)
        {
            if (graph == null)
            {
                throw new NyxException("can't serialize null value");
            }

            var fileName = Enum.GetName(typeof(CacheType), cacheType);
            try
            {
                _file.DeleteFile(fileName); /* this is because we need to display only 'startup data', not everything */
            }
            catch
            {
                /* Ignore, or log it? */
            }

            using (var stream = _file.CreateFile(fileName))
            {
                var strData = JsonConvert.SerializeObject(graph, Formatting.None);
                var arrData = Encoding.UTF8.GetBytes(strData);
                stream.Write(arrData, 0, arrData.Length);
            }
        }
        #endregion

        #region Available
        public bool Available(CacheType cacheType)
        {
            var fileName = Enum.GetName(typeof(CacheType), cacheType);
            return _file.FileExists(fileName);
        }
        #endregion

        #region Retrieve
        public TResult Retrieve<TResult>(CacheType cacheType)
        {
            var result = default(TResult);
            var fileName = Enum.GetName(typeof(CacheType), cacheType);

            try
            {
                using (var stream = _file.OpenFile(fileName, System.IO.FileMode.Open))
                {
                    using (var streamReader = new StreamReader(stream, Encoding.UTF8))
                    {
                        var strData = streamReader.ReadToEnd();
                        result = JsonConvert.DeserializeObject<TResult>(strData);
                    }
                }
            }
            catch
            {
                /* Ignore, or log it? */
            }

            return result;
        }
        #endregion
    }
}
